<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\AgentOrder
 *
 * @property int $id
 * @property int $agent_user_id agent_user_id
 * @property int $son_agent_user_id son_agent_user_id
 * @property int $buyer_user_id buyer_user_id
 * @property string $order_num order_num
 * @property float $agent_fee 结算费用
 * @property int $status 0待处理，1通过，2失败
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentOrder newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentOrder newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\AgentOrder onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentOrder query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentOrder whereAgentFee($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentOrder whereAgentUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentOrder whereBuyerUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentOrder whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentOrder whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentOrder whereOrderNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentOrder whereSonAgentUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentOrder whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentOrder whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\AgentOrder withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\AgentOrder withoutTrashed()
 * @mixin \Eloquent
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentOrder whereDeletedAt($value)
 */
class AgentOrder extends Model
{
    use SoftDeletes;
    public static function addAgentOrder($buyer_user_id,$order_num){
        if ($buyer_user_id == 0)
            return 0;
        if ($order_num == 0)
            return 0;
        $is_add = AgentOrder::whereOrderNum($order_num)->first();
        if ($is_add)
            return 0;
        $order = Orders::whereUserId($buyer_user_id)->whereOrderNum($order_num)->first();
        if (!$order)
            return 0;
        $order_item = OrderItem::whereOrderId($order->id)->first();
        if (!$order_item)
            return 0;
        $course = Course::whereId($order_item->course_id)->first();
        if (!$course)
            return 0;
        if ($course->is_agent_promotion == 0)
            return 0;
//        A->B,son_agent_user_id
        $buyer_is_b = AgentTeam::whereSonAgentUserId($buyer_user_id)->first();
        if ($buyer_is_b){
            $record = new AgentOrder();
            $record->agent_user_id = $buyer_is_b->agent_user_id;
            $record->son_agent_user_id =$buyer_is_b->son_agent_user_id;
            $record->buyer_user_id = $buyer_user_id;
            $record->order_num = $order_num;
            $record->agent_fee = round(($order->pay_amount * $course->first_ratio),2);
            $record->save();

//            $agent = AgentList::whereUserId($buyer_is_b->agent_user_id)->first();
//            if($agent){
//                $agent->balance += $record->agent_fee;
//                $agent->save();
//            }
            return 1;
        } else {
//            B->C
            $buyer_is_c = AgentTeam::whereSonUserId($buyer_user_id)->first();
            if ($buyer_is_c){
                $record = new AgentOrder();
                $record->agent_user_id      = $buyer_is_c->agent_user_id;
                $record->son_agent_user_id  = $buyer_is_c->son_agent_user_id;
                $record->buyer_user_id      = $buyer_user_id;
                $record->order_num          = $order_num;
                $record->agent_fee          = round(($order->pay_amount * $course->first_ratio),2);
                $record->save();
                return 1;
            }
        }

        return 0;
    }
}
